import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {NzMessageService, NzModalRef, NzModalService} from 'ng-zorro-antd';

@Injectable()
export class UtilService {
  readImgUrl = 'https://static.lessonplan.cn/';
  temUrl = 'https://templateserver.lessonplan.cn';
  appGuid = '38662c26-a07c-479c-a00e-3f4e7e9cc1b4';
  constructor(private http: HttpClient,
              private modal: NzModalService,
              private message: NzMessageService) {
  }

  comfirm(text: string, callback): void {
    let confirmModal: NzModalRef;
    confirmModal = this.modal.confirm({
      nzTitle: text,
      nzOnOk: () => {
        callback();
      }
    });
  }

  remind(type: string, text: string): void {
    this.message.create(type, text);
  }

  getUserInfo(): Promise<any> {
    // return new Promise((resolve, reject) => {
    //   resolve({status: 1, userGuid: '0c3d6e88-9f3e-4b57-ef5e-d3e3fec7130c'})
    // })
    return this.http.get(`/api/userInfo`).toPromise().then(res => {
      return res;
    })
  }

  logout(): Promise<any> {
    return this.http.get(`/logout`).toPromise().then(res => {
      return res;
    })
  }

  /*获取指定月份，年份，获取当年当月的最后一天的时间戳*/
  getDateTimeOfLastDay(year: number, month: number) {
    if (month == 12) {
      year += 1;
      month = 0;
    }
    let date = new Date();
    date.setFullYear(year);
    date.setMonth(month);
    date.setDate(1);
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    let time = (date.getTime() / 1000 - 1).toFixed(0);
    return time;
  }

  /*获取指定月份，年份，获取当年当月的第一天的时间戳*/
  getDateTimeOfFirstDay(year: number, month: number) {
    month = month - 1;
    let date = new Date();
    date.setFullYear(year);
    date.setMonth(month);
    date.setDate(1);
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    let time = (date.getTime() / 1000).toFixed(0);
    return time;
  }
  /*获取当前的时间戳*/
  getCurrentTime(){
    let nowdate = (new Date().getTime())/1000;
    return nowdate.toFixed(0);
  }
  /*路由守卫*/
  isCanEnterApp(UserGuid): Promise<any> {
    return this.http.get(`${this.temUrl}/PurchaseRecord/IsCanEnterApp?UserGuid=${UserGuid}&FK_AppGuid=${this.appGuid}`)
      .toPromise().then(res => {
        return res
      })
  }
}
